package org.haredot.controller;

import org.haredot.annotation.LogModule;
import org.haredot.entity.Menu;
import org.haredot.service.MenuService;
import org.haredot.vo.ResultVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@LogModule("菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService ;


    @GetMapping("/leftMenus")
    public ResultVo<?> leftMenus(Authentication authentication) {

        if (!authentication.isAuthenticated()) return ResultVo.fail("权限不足").build();

        // 获取 当前 登录的 用户名
        String username = authentication.getName();

        // 查询 当前登录的用户 对应的 左侧菜单权限
        return menuService.queryLeftMenus(username);

    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('menu:view:add')")
    public ResultVo<Menu> addMenu(@RequestBody Menu menu) {
        return menuService.saveMenu(menu);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('menu:view')")
    public ResultVo<List<Menu>> queryMenus() {
        return menuService.queryMenus() ;
    }

    @DeleteMapping("/del{menuId}")
    @PreAuthorize("hasAuthority('menu:view:remove')")
    public ResultVo<Menu> removeMenu(@PathVariable("menuId") Long id) {
        return menuService.removeMenu(id);
    }

    @PutMapping("/{menuId}")
    @PreAuthorize("hasAuthority('menu:view:modify')")
    public ResultVo<Menu> modifyMenu(@PathVariable("menuId") Long id,
                                     @RequestBody Menu menu) {
        menu.setId(id);
        return menuService.modifyMenu(menu);
    }
}
